package com.xtoad.ecms.baseinfo.service.impl;

import com.xtoad.ecms.baseinfo.converter.UserConverter;
import com.xtoad.ecms.baseinfo.dto.UserDTO;
import com.xtoad.ecms.baseinfo.model.User;
import com.xtoad.ecms.baseinfo.repository.IUserRepository;
import com.xtoad.ecms.baseinfo.service.IUserService;
import com.xtoad.ecms.common.web.exception.BusinessException;
import com.xtoad.ecms.common.web.exception.ResultCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * 用户服务实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private IUserRepository userRepository;
    @Resource
    private UserConverter userConverter;

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return 查询结果
     */
    @Override
    public UserDTO getUserById(final Long id) {
        Optional<User> user = userRepository.findById(id);
        if (user.isPresent()) {
            return userConverter.toDto(user.get());
        }

        throw new BusinessException(ResultCodeEnum.NOT_FOUND);
    }

    /**
     * 查询全部用户，不分页
     *
     * @return 查询结果
     */
    @Override
    public List<UserDTO> getAllUser() {
        List<User> allUsers = this.userRepository.findAll();
        return userConverter.toDtoList(allUsers);
    }

    /**
     * 新增用户
     *
     * @param userDTO 保存对象
     * @return 新增结果
     */
    @Override
    public UserDTO insert(final UserDTO userDTO) {
        User user = userConverter.toModel(userDTO);
        user = userRepository.save(user);

        return userConverter.toDto(user);
    }

    /**
     * 删除用户
     *
     * @param id 删除对象id
     */
    @Override
    public void deleteById(final Long id) {
        userRepository.deleteById(id);
    }

    /**
     * 删除用户
     *
     * @param userDTO 删除对象
     */
    @Override
    public void delete(final UserDTO userDTO) {
        User user = userConverter.toModel(userDTO);
        userRepository.delete(user);
    }

    /**
     * 更新用户
     *
     * @param userDTO 更新对象
     * @return 更新结果
     */
    @Override
    public UserDTO update(final UserDTO userDTO) {
        User user = userConverter.toModel(userDTO);
        user = userRepository.save(user);

        return userConverter.toDto(user);
    }

    /**
     * 批量新增用户
     *
     * @param userDTOList 新增对象
     * @return 新增结果
     */
    @Override
    public List<UserDTO> batchInsert(final List<UserDTO> userDTOList) {
        List<User> users = userConverter.toModelList(userDTOList);
        users = userRepository.saveAll(users);

        return userConverter.toDtoList(users);
    }

    /**
     * 批量删除用户
     *
     * @param userDTOList 删除对象
     */
    @Override
    public void batchDelete(final List<UserDTO> userDTOList) {
        List<User> users = userConverter.toModelList(userDTOList);
        userRepository.deleteInBatch(users);
    }

    /**
     * 批量更新用户
     *
     * @param userDTOList 更新对象
     * @return 更新结果
     */
    @Override
    public List<UserDTO> batchUpdate(final List<UserDTO> userDTOList) {
        List<User> users = userConverter.toModelList(userDTOList);
        users = userRepository.saveAll(users);

        return userConverter.toDtoList(users);
    }

    /**
     * 根据姓名查询用户
     *
     * @param name 用户姓名
     * @return 查询结果
     */
    @Override
    public List<UserDTO> getUserWithName(final String name) {
        List<User> users = this.userRepository.findByNameContaining(name);

        return userConverter.toDtoList(users);
    }

    /**
     * 根据登录名查询用户信息
     *
     * @param loginNo 登录名
     * @return 查询结果
     */
    @Override
    public UserDTO getUserByLoginNo(final String loginNo) {
        User user = userRepository.findByEmailOrPhoneOrLoginNo(loginNo, loginNo, loginNo);
        if (user == null) {
            return null;
        }

        return userConverter.toDto(user);
    }

}
